const gulp = require('gulp');
const del = require('del');
const sass = require('gulp-sass');
const autoprefixer = require('gulp-autoprefixer');
const browserSync = require('browser-sync');
const reload = browserSync.reload;
const plumber = require('gulp-plumber');
const notify = require('gulp-notify');
const cleanCSS = require('gulp-clean-css');
const concat = require('gulp-concat');
const uglify = require('gulp-uglify');

const SRC = 'src/'

const OPTIONS = {
	DIST: {
		src: SRC,
		css: function() {
			return this.src + 'css/';
		},
		script: function() {
			return this.src + 'script/'
		},
		getSrc: function() {
            return this.src;
        }
	},
	APP: {
		src: SRC,
        scss: function() {
            return this.src + 'scss/';
        },
        script: function() {
            return this.src + 'script/';
        }
	}
}

// 1.清空
gulp.task('clean', function(cb) {
    del([OPTIONS.DIST.getSrc()+ '**/*'], cb);
});

// 2.编译scss
gulp.task('scss', function() {
    return gulp.src([OPTIONS.APP.scss() + '**/*.scss'])
        .pipe(plumber({errorHandler: notify.onError('Error: <%= error.message %>')}))
        .pipe(sass({outputStyle: 'expanded'}))
        .pipe(autoprefixer(
            {
                browsers: ['last 2 version', '> 5%', 'safari > 5','chrome > 35', 'ff > 10', 'opera > 10', 'ie > 6'],
                cascade: false
            }
        ))
        // .pipe(cleanCSS())
        .pipe(gulp.dest(OPTIONS.DIST.css()))
        .pipe(reload({stream: true}));
});

gulp.task('html', function() {
    return gulp.src([OPTIONS.SRC + '**/*.html', OPTIONS.SRC + '*.html'])
        .pipe(reload({stream: true}));
});

// 4.js
gulp.task('script', function() {
	return gulp.src([OPTIONS.APP.script() + '**/*.js'])
		.pipe(gulp.dest(OPTIONS.DIST.script()))
		.pipe(reload({stream: true}));
})

// 3.browser-sync，自动刷新页面
gulp.task('browser-sync', ['html', 'scss'], function () {
    browserSync.init({
        server: {
            baseDir: [SRC]
        }
    });
    gulp.watch([OPTIONS.SRC + '**/*.html', OPTIONS.SRC + '*.html'], ['html']);
    gulp.watch([OPTIONS.APP.scss()+ '**/*.scss'], ['scss']);
    // gulp.watch([OPTIONS.APP.script()+ '**/*.js'], ['script']);
});

gulp.task('default', ['browser-sync'], function() {

});